﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using YFIN.Admin.RiskPreferQuestionServiceRef;
using Microsoft.Expression.Interactivity.Core;
using YFIN.Admin.Admin;

namespace YFIN.Admin.ViewModels.Admin
{
    public class RiskPreferManageVM : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion

        #region 属性
        private ObservableCollection<ModelRiskPreferQuestion> _questions;
        public ObservableCollection<ModelRiskPreferQuestion> Questions
        {
            get
            {
                return _questions;
            }
            set
            {
                if (_questions != value)
                {
                    _questions = value;
                    NotifyPropertyChanged("Questions");
                }
            }
        }

       private ModelRiskPreferQuestion _currentQuestion;
        public ModelRiskPreferQuestion CurrentQuestion
        {
            get
            {
                return _currentQuestion;
            }
            set
            {
                if (_currentQuestion != value)
                {
                    _currentQuestion = value;
                    NotifyPropertyChanged("CurrentQuestion");
                }
            }
        }

        private ModelRiskPreferAnswer _currentAnswer;
        public ModelRiskPreferAnswer CurrentAnswer
        {
            get
            {
                return _currentAnswer;
            }
            set
            {
                if (_currentAnswer != value)
                {
                    _currentAnswer = value;
                    NotifyPropertyChanged("CurrentAnswer");
                }
            }
        }
       

        public ICommand AddQuestionCommand { get; private set; }
        public ICommand UpdateQuestionCommand { get; private set; }
        public ICommand DeleteQuestionCommand { get; private set; }
        #endregion

        public RiskPreferManageVM()
        {
            AddQuestionCommand = new ActionCommand(AddQuestion);
            UpdateQuestionCommand = new ActionCommand(UpdateQuestion);
            DeleteQuestionCommand = new ActionCommand(DeleteQuestion);

            var client = new RiskPreferQuestionServiceClient();
            client.GetAllCompleted +=new EventHandler<GetAllCompletedEventArgs>(client_GetAllCompleted);
            client.GetAllAsync();
        }

        public void AddQuestion()
        {
            CurrentQuestion = new ModelRiskPreferQuestion
                                  {
                                      Time = DateTime.Now,
                                      Author = MainPage.Admin.AdminName,
                                      Answers = new ObservableCollection<ModelRiskPreferAnswer>()
                                      };
            var m = new RiskPreferQuestionDetail { DataContext = CurrentQuestion };
            m.Show();
            m.Closed +=
                (sender, e) =>
                {
                    if (m.DialogResult.HasValue && m.DialogResult.Value)
                    {
                        var client = new RiskPreferQuestionServiceClient();
                        client.AddCompleted += new EventHandler<RiskPreferQuestionServiceRef.AddCompletedEventArgs>(client_AddQuestionCompleted);
                        client.AddAsync(CurrentQuestion);
                    }
                };
        }

        public void UpdateQuestion()
        {
            var m = new RiskPreferQuestionDetail { DataContext = CurrentQuestion };
            m.Show();
            m.Closed +=
                (sender, e) =>
                {
                    if (m.DialogResult.HasValue && m.DialogResult.Value)
                    {
                        var client = new RiskPreferQuestionServiceClient();
                        client.UpdateCompleted += new EventHandler<UpdateCompletedEventArgs>(client_UpdateQuestionCompleted);
                        client.UpdateAsync(CurrentQuestion);
                    }
                };
        }

        public void DeleteQuestion()
        {
            var client = new RiskPreferQuestionServiceClient();
            client.DeleteCompleted += new EventHandler<DeleteCompletedEventArgs>(client_DeleteQuestionCompleted);
            client.DeleteAsync(CurrentQuestion);
        }

        public void AddAnswer(ModelRiskPreferAnswer propertyContent)
        {
            CurrentAnswer = propertyContent;
            var m = new RiskPreferAnswerDetail { DataContext = CurrentAnswer };
            m.Show();
            m.Closed +=
                (sender, e) =>
                {
                    if (m.DialogResult.HasValue && m.DialogResult.Value)
                    {
                        var client = new RiskPreferQuestionServiceClient();
                        client.AddAnswerCompleted +=
                            new EventHandler<AddAnswerCompletedEventArgs>(client_AddAnswerCompleted);
                        client.AddAnswerAsync(CurrentAnswer);
                    }
                };
        }

        public void UpdateAnswer(object selectedItem)
        {
            CurrentAnswer = (ModelRiskPreferAnswer)selectedItem;
            var m = new RiskPreferAnswerDetail { DataContext = CurrentAnswer };
            m.Show();
            m.Closed +=
                (sender, e) =>
                {
                    if (m.DialogResult.HasValue && m.DialogResult.Value)
                    {
                        var client = new RiskPreferQuestionServiceClient();
                        client.UpdateAnswerCompleted += new EventHandler<UpdateAnswerCompletedEventArgs>(client_UpdateAnswerCompleted);
                        client.UpdateAnswerAsync(CurrentAnswer);
                    }
                };
        }

        public void DeleteAnswer()
        {
            var client = new RiskPreferQuestionServiceClient();
            client.DeleteAnswerCompleted += new EventHandler<DeleteAnswerCompletedEventArgs>(client_DeleteAnswerCompleted);
            client.DeleteAnswerAsync(CurrentAnswer);
        }

        private void client_GetAllCompleted(object sender, GetAllCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                Questions = e.Result;
            }
        }

        private void client_GetAllCompleted2(object sender, GetAllCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
            {
                Questions.Add(e.Result[e.Result.Count - 1]);
            }
        }

        private void client_AddQuestionCompleted(object sender, AddCompletedEventArgs e)
        {
            if (e.Result)
            {
                var client = new RiskPreferQuestionServiceClient();
                client.GetAllCompleted += new EventHandler<GetAllCompletedEventArgs>(client_GetAllCompleted2);
                client.GetAllAsync();
                CurrentQuestion = null;
            }
        }

        private void client_UpdateQuestionCompleted(object sender, UpdateCompletedEventArgs e)
        {
            if (!e.Result)
            {
                MessageBox.Show("修改失败");
            }
        }

        private void client_DeleteQuestionCompleted(object sender, DeleteCompletedEventArgs e)
        {
            try
            {
                if (e.Result)
                {
                    Questions.Remove(CurrentQuestion);
                    CurrentQuestion = null;
                }
            }
            catch (Exception ex)
            {
                //TODO 莫名其妙的异常
            }
        }

        private void client_AddAnswerCompleted(object sender, AddAnswerCompletedEventArgs e)
        {
            if (e.Result)
            {
                var client = new RiskPreferQuestionServiceClient();
                client.GetAnswerByQuestionNoCompleted +=
                    new EventHandler<GetAnswerByQuestionNoCompletedEventArgs>(client_GetAnswerByQuestionNoCompleted);
                client.GetAnswerByQuestionNoAsync(CurrentAnswer.QuestionNo);
            }
        }

        private void client_GetAnswerByQuestionNoCompleted(object sender, GetAnswerByQuestionNoCompletedEventArgs e)
        {
            try
            {
                if (e.Result != null && e.Result.Count > 0)
                {
                    Questions.SingleOrDefault(pt => pt.No == CurrentAnswer.QuestionNo).Answers.Add
                        (e.Result[e.Result.Count - 1]);
                    Questions.SingleOrDefault(pt => pt.No == CurrentAnswer.QuestionNo).AnswerToAdd =
                        new ModelRiskPreferAnswer { QuestionNo = CurrentAnswer.QuestionNo };
                    CurrentAnswer = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void client_UpdateAnswerCompleted(object sender, UpdateAnswerCompletedEventArgs e)
        {
            if (!e.Result)
            {
                MessageBox.Show("修改失败");
            }
        }

        private void client_DeleteAnswerCompleted(object sender, DeleteAnswerCompletedEventArgs e)
        {
            if (e.Result)
            {
                Questions.SingleOrDefault(pt => pt.No == CurrentAnswer.QuestionNo).
                    Answers.Remove(CurrentAnswer);
                CurrentAnswer = null;
            }
        }
    }
}
